package com.live.shop.sql;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.live.shop.model.CommentModel;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class CommentsListDatabase {
	private static MyDatabaseHelper helper;
	public CommentsListDatabase(Context context){
		//创建数据库和表
		helper = new MyDatabaseHelper(context, "user.db",null,3,null);
	}
	@SuppressLint("Range")
	public static List<CommentModel> selectAll() {
		List<CommentModel> list = new ArrayList<>();
		SQLiteDatabase database = helper.getReadableDatabase();
		Cursor cursor = database.rawQuery("select * from comments_list order by create_time desc", null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				CommentModel commentModel = new CommentModel();
				commentModel.setCommentsId(cursor.getInt(cursor.getColumnIndex("comments_id")));
				commentModel.setCommentsUser(cursor.getString(cursor.getColumnIndex("comments_user")));
				commentModel.setCommentsTxt(cursor.getString(cursor.getColumnIndex("comments_txt")));
				commentModel.setCommentsMark(cursor.getInt(cursor.getColumnIndex("comments_mark")));
				commentModel.setCommentsType(cursor.getInt(cursor.getColumnIndex("comments_type")));
				commentModel.setKeywordId(cursor.getInt(cursor.getColumnIndex("keyword_id")));
				commentModel.setKeywordMate(cursor.getInt(cursor.getColumnIndex("keyword_mate")));
				commentModel.setKeywordReply(cursor.getInt(cursor.getColumnIndex("keyword_reply")));
				commentModel.setKeywordAudio(cursor.getString(cursor.getColumnIndex("keyword_audio")));
				commentModel.setCreateTime(cursor.getString(cursor.getColumnIndex("create_time")));
				list.add(commentModel);
			}
			cursor.close();
		}
		database.close();
		return list;
	}
	//查找匹配未回复的：匹配状态 0未匹配 1无匹配 2 问答有匹配 3素材有匹配
	@SuppressLint("Range")
	public static CommentModel selectKeywordMateOld(int keyword_mate) {
		SQLiteDatabase database = helper.getReadableDatabase();
		Cursor cursor = database.rawQuery("select * from comments_list where keyword_mate = "+keyword_mate+" and keyword_reply = 0 order by create_time asc limit 1", null);
		CommentModel commentModel = new CommentModel();
		commentModel.setKeywordId(0);
		if (cursor != null && cursor.getCount() > 0) {
			if (cursor.moveToNext()) {
				commentModel.setCommentsId(cursor.getInt(cursor.getColumnIndex("comments_id")));
				commentModel.setCommentsUser(cursor.getString(cursor.getColumnIndex("comments_user")));
				commentModel.setCommentsTxt(cursor.getString(cursor.getColumnIndex("comments_txt")));
				commentModel.setCommentsMark(cursor.getInt(cursor.getColumnIndex("comments_mark")));
				commentModel.setCommentsType(cursor.getInt(cursor.getColumnIndex("comments_type")));
				commentModel.setKeywordId(cursor.getInt(cursor.getColumnIndex("keyword_id")));
				commentModel.setKeywordMate(cursor.getInt(cursor.getColumnIndex("keyword_mate")));
				commentModel.setKeywordReply(cursor.getInt(cursor.getColumnIndex("keyword_reply")));
				commentModel.setKeywordAudio(cursor.getString(cursor.getColumnIndex("keyword_audio")));
				commentModel.setCreateTime(cursor.getString(cursor.getColumnIndex("create_time")));
			}
			cursor.close();
		}
		database.close();
		return commentModel;
	}
	// 插入一条数据
	@SuppressLint("SimpleDateFormat")
	public static long insertData2(String comments_user,String comments_txt,int comments_type,int keyword_mate,String keyword_audio) {
		SQLiteDatabase database = helper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("comments_user", comments_user);
		values.put("comments_txt", comments_txt);
		values.put("comments_type", comments_type);
		values.put("keyword_mate", keyword_mate);
		values.put("keyword_audio", keyword_audio);
		values.put("create_time", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
		long comid = database.insert("comments_list", null, values);
		values.clear();
		database.close();
		return comid;
	}

	//更改关键词ID
	@SuppressLint("Range")
	public static void updateKeywordId(int comid,int keyword_id,int keyword_mate) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE comments_list SET keyword_id="+keyword_id+",keyword_mate = "+keyword_mate+" WHERE comments_id = "+comid);
		database.close();
	}
	//更改回复状态
	@SuppressLint("Range")
	public static void updateKeywordReply(long comid,int keyword_reply) {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("UPDATE comments_list SET keyword_reply = "+keyword_reply+" WHERE comments_id = "+comid);
		database.close();
	}
	//更改标记
	@SuppressLint("Range")
	public static void updateMark(CommentModel commentModel) {
		SQLiteDatabase database = helper.getReadableDatabase();
		ContentValues values = new ContentValues();
		values.put("comments_mark", commentModel.getCommentsMark());
		database.update("comments_list", values, "comments_id = ?", new String[] {String.valueOf(commentModel.getCommentsId())});
		values.clear();
		database.close();
	}
	//删除未标记的数据
	public static void deleteNoMark() {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("delete FROM comments_list where comments_mark=0");
		database.close();
	}
	//删除全部
	public static void deleteAll() {
		SQLiteDatabase database = helper.getReadableDatabase();
		database.execSQL("delete FROM comments_list");
		database.close();
	}
}
